package com.jeff.utils;

import android.text.TextUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.StringTokenizer;
import java.util.Vector;

/**
 * author：duff
 * version：1.0.0
 * date：2017/8/31
 */
public class DateUtil {
    public static final long MINUTE = 60 * 1000; // 分
    public static final long HOUR = 60 * MINUTE; // 小时
    public static final long DAY = HOUR * 24; // 天
    public static final long WEEK = DAY * 7; // 周
    public static final long MONTH = DAY * 30; // 月

    /**
     * @return 年－月－日 小时:分:秒
     */
    public static String getDateTimeString(Date day) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(day);

        return dateString.trim();
    }

    /**
     * @param sepSign 日期分割符号
     * @return 年 sepSign 月 sepSign 日
     */
    public static String getDateString(String sepSign) {
        SimpleDateFormat formatter;
        formatter = new SimpleDateFormat("yyyy" + sepSign + "MM" + sepSign + "dd");
        String dateString = formatter.format(new Date());

        return dateString.trim();
    }

    /**
     * @param sepSign 日期分割符号
     * @return 年 sepSign 月 sepSign 日
     */
    public static String getDateString(Date date, String sepSign) {
        SimpleDateFormat formatter;
        formatter = new SimpleDateFormat("yyyy" + sepSign + "MM" + sepSign + "dd");
        String dateString = formatter.format(date);

        return dateString.trim();
    }

    /**
     * @param dateStr 格式 yyyy-MM-dd HH:mm:ss
     * @return yyyy-MM-dd
     */
    public static String getDateValue(String dateStr) {
        dateStr = dateStr.trim();

        int in = dateStr.indexOf(" ");
        if (in > 0)
            return dateStr.substring(0, in).trim();

        return dateStr;
    }

    /**
     * @param value yyyy-MM-dd HH:mm:ss
     * @return Date 对象
     */
    public static Date formatLongDate(String value) {
        if (value == null)
            return null;
        try {
            Long dvalue = Long.parseLong(value);
            Date d = new Date(dvalue);
            return d;
        } catch (Exception e) {
            return null;
        }
    }


    /**
     *
     * @return yyyy年MM月
     */
    public static String getYearMonth() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月");
        String dateString = formatter.format(new Date());

        return dateString.trim();
    }

    /**
     *
     * @param date
     * @return yyyy年MM月
     */
    public static String getYearMonth(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月");
        String dateString = formatter.format(date);

        return dateString.trim();
    }

    /**
     *
     * @return dd日
     */
    public static String getDay() {
        SimpleDateFormat formatter = new SimpleDateFormat("dd日");
        String dateString = formatter.format(new Date());

        return dateString.trim();
    }

    /**
     *
     * @param date
     * @return dd日
     */
    public static String getDay(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("dd日");
        String dateString = formatter.format(date);

        return dateString.trim();
    }

    /**
     *
     * @param date
     * @param formatValue 如： yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String getFormatDate(Date date, String formatValue) {
        SimpleDateFormat formatter = new SimpleDateFormat(formatValue);
        String dateString = formatter.format(date);

        return dateString.trim();
    }

    /**
     * 两天是否再在同一天
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isDifferentDay(Date date1, Date date2) {
        try {
            String date1Value = getDateTimeString(date1);
            String date2Value = getDateTimeString(date2);
            if (date1Value.equals(date2Value))
                return false;

            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     *
     * @param date
     * @return HH:mm:ss
     */
    public static String getTimeValue(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
        String dateString = formatter.format(date);

        return dateString.trim();
    }

    /**
     *
     * @return HH:mm:ss
     */
    public static String getCurrentTime() {
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
        Date date = new Date();
        String dateString = formatter.format(date);

        return dateString.trim();
    }

    /**
     *
     * @param cal
     * @return HH:mm:ss
     */
    public static String getTime(Calendar cal) {
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
        Date date = cal.getTime();
        String dateString = formatter.format(date);

        return dateString.trim();
    }

    /**
     *
     * @param date
     * @return HH:mm
     */
    public static String getHourMinute(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
        String dateString = formatter.format(date);
        return dateString.trim();
    }


    /**
     * @return
     */
    public static Date getNowDate() {
        return new Date();
    }


    /**
     * 下一天
     * @param date
     * @return
     */
    public static Date nextDay(Date date) {
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.add(Calendar.DATE, 1);

        return cal.getTime();
    }


    /**
     * 加上几天
     * @param date
     * @param days
     * @return Date
     */
    public static Date addDays(Date date, int days) {
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.add(Calendar.DATE, days);

        return cal.getTime();
    }

    /**
     * @param dateStr
     * @param days
     * @return yyyy-MM-dd
     */
    public static String addDays(String dateStr, int days) {
        Date date = getStringDateToDate(dateStr);
        date = addDays(date, days);

        SimpleDateFormat formatter;
        formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(date);

        return dateString.trim();
    }

    /**
     *
     * @param dateStr
     * @param days
     * @param sep
     * @return "yyyy" + sep + "MM" + sep + "dd"
     */
    public static String addDays(String dateStr, int days, String sep) {
        try {
            Date date = getStringDateToDate(dateStr);
            date = addDays(date, days);
            SimpleDateFormat formatter;
            formatter = new SimpleDateFormat("yyyy" + sep + "MM" + sep + "dd");
            String dateString = formatter.format(date);
            return dateString.trim();
        } catch (Exception e) {
            return "";
        }
    }

    /**
     *
     * @param date
     * @param days
     * @param sep
     * @return "yyyy" + sep + "MM" + sep + "dd"
     */
    public static String addDays(Date date, int days, String sep) {
        date = addDays(date, days);

        SimpleDateFormat formatter;
        formatter = new SimpleDateFormat("yyyy" + sep + "MM" + sep + "dd");
        String dateString = formatter.format(date);

        return dateString.trim();
    }

    /**
     * 将特殊分割符的日期转换成Date对象
     * @param dateStr
     * @param formatStr
     * @return
     */
    public static Date convert2Date(String dateStr, String formatStr) {
        Date d = null;
        SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
        try {
            d = sdf.parse(dateStr);
        } catch (ParseException e) {
        }

        return d;
    }

    /**
     * 取得String 日期中的月数据
     * @param date yyyy-MM-dd HH:mm:ss / yyyy-MM-dd
     * @return
     */
    public static String getDateMonth(String date) {
        String month = "";

        StringTokenizer filter = new StringTokenizer(date, "-");
        month = filter.nextToken();

        return month;
    }

    /**
     * 取得String 日期中的年数据
     * @param date yyyy-MM-dd HH:mm:ss / yyyy-MM-dd
     * @return
     */
    public static String getDateYear(String date) {
        String year = "";

        StringTokenizer filter = new StringTokenizer(date, "-");
        year = filter.nextToken();

        return year;
    }


    /**
     * 取得月份Vector
     * @return
     */
    public static Vector getMonthVector() {
        Vector v = new Vector();
        v.add("1");
        v.add("2");
        v.add("3");
        v.add("4");
        v.add("5");
        v.add("6");
        v.add("7");
        v.add("8");
        v.add("9");
        v.add("10");
        v.add("11");
        v.add("12");

        return v;
    }

    /**
     * 取得两位长度的月份Vector
     * @return
     */
    public static Vector getZeroMonthVector() {
        Vector v = new Vector();
        v.add("01");
        v.add("02");
        v.add("03");
        v.add("04");
        v.add("05");
        v.add("06");
        v.add("07");
        v.add("08");
        v.add("09");
        v.add("10");
        v.add("11");
        v.add("12");

        return v;
    }


    private static Date getStringDateToDate(String dateStr) {

        String dvalue = dateStr.trim();
        int index = dvalue.indexOf(" ");
        if (index > 0) {
            // 如果日期中有小时等信息
            dateStr = dateStr.substring(0, index);
        }

        StringTokenizer filter = new StringTokenizer(dateStr, "-");
        int year = Integer.parseInt(filter.nextToken());
        int mon = Integer.parseInt(filter.nextToken()) - 1;
        int day = Integer.parseInt(filter.nextToken());

        Calendar cal = new GregorianCalendar(year, mon, day);
        return cal.getTime();
    }


    /**
     * 用秒的方式取得当前的时间
     *
     * @return
     */
    public static long getNowTime() {
        Date date = new Date();

        return date.getHours() * 3600 + date.getMinutes() * 60 + date.getMinutes();
    }

    /**
     * 将long型的时间转换成String格式
     * @param timeLong
     * @return
     */
    public static String formatDateLongToString(Long timeLong) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String timeString;
        timeString = dateFormat.format(new Date(timeLong));
        return timeString;
    }

    /**
     * 将String型的时间转换成Long格式
     * @param timeString
     * @return
     */
    public static long formatDateStringToLong(String timeString) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long timeLong = 0;
        try {
            timeLong = dateFormat.parse(timeString).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return timeLong;
    }

    /**
     * 将String型的时间转换成Long格式
     * @param timeString
     * @return
     */
    public static long formatDateStringToLong(String timeString, String format) {
        if (TextUtils.isEmpty(format)) {
            return formatDateStringToLong(timeString);
        }
        DateFormat dateFormat = new SimpleDateFormat(format, Locale.CHINA);
        long timeLong;
        try {
            timeLong = dateFormat.parse(timeString).getTime();
        } catch (ParseException e) {
            timeLong = formatDateStringToLong(timeString);
        }
        return timeLong;
    }

    /**----------------星期 Start---------------------**/

    /**
     * 计算星期几
     *
     * @param dateValue
     * @return
     */
    public static int getDayOfWeek(String dateValue) {
        if (dateValue == null || dateValue.equals(""))
            return -1;

        if (dateValue.trim().indexOf(" ") > 0 && dateValue.indexOf(":") > 0) {
            return getDayOfWeekFullTime(dateValue);
        } else {
            return getDayOfWeekNoTime(dateValue);
        }
    }

    /**
     *
     *
     * @param dateValue YYYY-MM-DD
     * @return
     */
    public static int getDayOfWeekNoTime(String dateValue) {
        try {
            Calendar calendar = Calendar.getInstance();
            Date date = new Date();

            SimpleDateFormat sdfInput = new SimpleDateFormat("yyyy-MM-dd");
            date = sdfInput.parse(dateValue);

            calendar.setTime(date);
            int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
            return dayOfWeek;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return -1;
    }

    /**
     *计算星期几
     *
     * @param dateValue YYYY-MM-DD HH:mm:ss
     * @return
     */
    public static int getDayOfWeekFullTime(String dateValue) {
        try {
            Calendar calendar = Calendar.getInstance();
            Date date = new Date();

            SimpleDateFormat sdfInput = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            date = sdfInput.parse(dateValue);

            calendar.setTime(date);
            int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
            return dayOfWeek;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return -1;
    }

    /**----------------星期 End---------------------**/


    /**--------------- 时间差别 Start--------------------**/

    /**
     * 两天时间的差异
     * @param date1
     * @param date2
     * @return
     */
    public static long differenceInDays(Calendar date1, Calendar date2) {
        final long msPerDay = 1000 * 60 * 60 * 24;

        final long date1Milliseconds = date1.getTime().getTime();
        final long date2Milliseconds = date2.getTime().getTime();
        final long result = (date1Milliseconds - date2Milliseconds) / msPerDay;

        return result;
    }

    /**
     * 两个日期差的天数
     * @param dateStr1
     * @param dateStr2
     * @return
     */
    public static long differenceDays(String dateStr1, String dateStr2) {
        try {
            Date date1 = getStringDateToDate(dateStr1);
            Date date2 = getStringDateToDate(dateStr2);

            Calendar cal1 = new GregorianCalendar();
            cal1.setTime(date1);

            Calendar cal2 = new GregorianCalendar();
            cal2.setTime(date2);

            return differenceInDays(cal1, cal2);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }


    /**
     * 两个日期差的天数
     * @param date1
     * @param date2
     * @return
     */
    public static long getDifferenceDays(Date date1, Date date2) {
        try {
            Calendar cal1 = new GregorianCalendar();
            cal1.setTime(date1);

            Calendar cal2 = new GregorianCalendar();
            cal2.setTime(date2);

            return differenceInDays(cal1, cal2);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }


    /**
     *
     * @param date1
     * @param date2
     * @return
     */
    public static long difference(Date date1, Date date2) {
        return date1.getTime() - date2.getTime();
    }

    /**
     * 取得String 日期中的日数据
     * @param date yyyy-MM-dd HH:mm:ss / yyyy-MM-dd
     * @return
     */
    public static String getDateDay(String date) {
        String day = "";

        StringTokenizer filter = new StringTokenizer(date, "-");
        day = filter.nextToken();

        return day;
    }


    /**
     * 两个时间相差的小时数
     * @param date1
     * @param date2
     * @return
     */
    public static long getDifferenceHours(Date date1, Date date2) {
        return DateUtil.difference(date1, date2) / 3600000;
    }

    /**
     * 两个时间相差的分钟数
     * @param date1
     * @param date2
     * @return
     */
    public static long getDifferenceMinutes(Date date1, Date date2) {
        return DateUtil.difference(date1, date2) / 60000;
    }

    /**
     * 两个时间相差的秒数
     * @param date1
     * @param date2
     * @return
     */
    public static long getDifferenceSeconds(Date date1, Date date2) {
        return difference(date1, date2) / 1000;
    }

    /**
     * 取得两个时间的相差秒数
     * @param date1
     * @param date2
     * @return
     */
    public static long getTimeSpaceSeconds(Date date1, Date date2) {
        return (date1.getDay() - date2.getDay()) * 12 * 3600 + (date1.getHours() - date2.getHours()) * 3600 + (date1.getMinutes() - date2.getMinutes()) * 60 + (date1.getSeconds() - date2.getSeconds());
    }

    /**
     * 取得两个时间相差毫秒数
     * @param date1
     * @param date2
     * @return
     */
    public static long getTimeSpaceMinSeconds(Date date1, Date date2) {
        return date1.getTime() - date2.getTime();
    }

    /**
     * @param dateStr1
     * @param dateStr2
     * @param formatValue
     * @return dateStr1 is after dateStr2 ?
     */
    public static boolean isDateAfter(String dateStr1, String dateStr2, String formatValue) {
        boolean isBigger = false;
        SimpleDateFormat sdf = new SimpleDateFormat(formatValue);
        Date date1 = null;
        Date date2 = null;
        try {
            date1 = sdf.parse(dateStr1);
            date2 = sdf.parse(dateStr2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (date1 == null || date2 == null)
            return isBigger;

        if (date1.getTime() > date2.getTime()) {
            isBigger = true;
        } else if (date1.getTime() < date2.getTime()) {
            isBigger = false;
        }
        return isBigger;
    }

    /**
     * @return 当天24点时间
     */
    public static long getTime24() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 24);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTimeInMillis();
    }

}
